//
//  LyNextViewController.m
//  MailList
//
//  Created by dllo on 15/11/21.
//  Copyright © 2015年 KPY. All rights reserved.
//

#import "LyNextViewController.h"
#import "LyMailListManager.h"
#import "Student.h"
@interface LyNextViewController ()

@property(nonatomic, strong) LyMailListManager *manager;

@property(nonatomic, strong) UILabel *nameLabel;

@property(nonatomic, strong) UILabel *sexLabel;

@property(nonatomic, strong) UILabel *phoneLabel;

@property(nonatomic, strong) UILabel *placeLabel;

@property(nonatomic, strong) UILabel *qqLabel;

@property(nonatomic, strong) UILabel *numberLabel;

@property(nonatomic, strong) UITextField *nameText;

@property(nonatomic, strong) UITextField *sexText;

@property(nonatomic, strong) UITextField *phoneText;

@property(nonatomic, strong) UITextField *placeText;

@property(nonatomic, strong) UITextField *qqText;

@property(nonatomic, strong) UITextField *numberText;

@end

@implementation LyNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 创建数据管理器
    self.manager = [LyMailListManager defaults];
    
    NSLog(@"%@", [self.manager applicationDocumentsDirectory]);
    
    [self.manager applicationDocumentsDirectory];
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(updateAction:)];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 50, 70, 40)];
    
    self.nameLabel.text = @"姓名";
    
    self.nameLabel.textAlignment = 1;
    
    self.nameLabel.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.nameLabel];
    
    self.nameText = [[UITextField alloc] initWithFrame:CGRectMake(150, 50, 200, 40)];
    
    self.nameText.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.nameText];
    
    
    
    self.sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 120, 70, 40)];
    
    self.sexLabel.text = @"性别";
    
    self.sexLabel.textAlignment = 1;
    
    self.sexLabel.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.sexLabel];
    
    self.sexText = [[UITextField alloc] initWithFrame:CGRectMake(150, 120, 200, 40)];
    
    self.sexText.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.sexText];
    
    
    
    self.numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 190, 70, 40)];
    
    self.numberLabel.text = @"学号";
    
    self.numberLabel.textAlignment = 1;
    
    self.numberLabel.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.numberLabel];
    
    self.numberText = [[UITextField alloc] initWithFrame:CGRectMake(150, 190, 200, 40)];
    
    self.numberText.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.numberText];
    
    
    
    self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 260, 70, 40)];
    
    self.phoneLabel.text = @"电话";
    
    self.phoneLabel.textAlignment = 1;
    
    self.phoneLabel.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.phoneLabel];
    
    self.phoneText = [[UITextField alloc] initWithFrame:CGRectMake(150, 260, 200, 40)];
    
    self.phoneText.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.phoneText];
    
    
    
    self.qqLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 330, 70, 40)];
    
    self.qqLabel.text = @"QQ";
    
    self.qqLabel.textAlignment = 1;
    
    self.qqLabel.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.qqLabel];
    
    self.qqText = [[UITextField alloc] initWithFrame:CGRectMake(150, 330, 200, 40)];
    
    self.qqText.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.qqText];
    
    
    
    self.placeLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 400, 70, 40)];
    
    self.placeLabel.text = @"籍贯";
    
    self.placeLabel.textAlignment = 1;
    
    self.placeLabel.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.placeLabel];
    
    self.placeText = [[UITextField alloc] initWithFrame:CGRectMake(150, 400, 200, 40)];
    
    self.placeText.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.placeText];
    
    
    

    
    
}

- (void)updateAction:(UIBarButtonItem *)button {
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    
    Student *stu = [[Student alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    
    stu.name = self.nameText.text;
    
    stu.sex = self.sexText.text;
    
    NSNumberFormatter * qq = [[NSNumberFormatter alloc] init];
    [qq setNumberStyle:NSNumberFormatterDecimalStyle];
    
    stu.qq = [qq numberFromString:self.qqText.text];
    
    NSNumberFormatter * number = [[NSNumberFormatter alloc] init];
    [number setNumberStyle:NSNumberFormatterDecimalStyle];
    
    stu.number = [number numberFromString:self.numberText.text];
    
    NSNumberFormatter * phone = [[NSNumberFormatter alloc] init];
    [phone setNumberStyle:NSNumberFormatterDecimalStyle];
    
    stu.phone = [phone numberFromString:self.phoneText.text];
    
    stu.place = self.placeText.text;
    
    [self.manager saveContext];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
